package com.irun.pm.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.irun.pm.R;
import com.irun.pm.utils.FileUtils;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.Utils;


public class PoliceMobileActivity extends Activity {
    
	private int[] mThumbs = {
		R.drawable.grid_population_query, R.drawable.grid_fugitives, R.drawable.grid_violation_query,
		R.drawable.grid_carinfo, R.drawable.grid_driver_query, R.drawable.grid_site_enforcement,
		R.drawable.grid_accident_presentation, R.drawable.grid_system_manager, R.drawable.grid_help
	};
	
	private String[] intents;
	private Context mContext;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        String uname = PreferenceManager.getDefaultSharedPreferences(this).getString(Utils.PREFERENCES_NAME_KEY, "");
        if(StringUtil.isEmpty(uname))
        	uname = getIntent().getStringExtra(Utils.PREFERENCES_NAME_KEY);
        TextView user = (TextView) findViewById(R.id.titleBar_uname);
        user.setText("登录交警  " + uname);
        TextView exit = (TextView) findViewById(R.id.titleBar_date);
        exit.setOnClickListener(new TextView.OnClickListener(){
        	@Override
        	public void onClick(View v) {
        		showDialog(1);
        	}
        });
        
        mContext = this;
        intents = getResources().getStringArray(R.array.app_intents);
        
        GridView gridView = (GridView) findViewById(R.id.main_grid);
        gridView.setAdapter(new ItemAdapter(mContext));
        gridView.setOnItemClickListener(new GridView.OnItemClickListener(){
        	@Override
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		if(position == 6 || position == 7|| position == 8){
        			return;
        		}
        		if(intents[position] != null){
        			Intent intent = new Intent();
    				intent.setClassName(getPackageName(), intents[position]);
    				startActivity(intent);
        		}
        	}
        });
        FileUtils.createMediaFile();
    }
    
    /***
     * 数据适配�?     * @author Administrator
     */
    public class ItemAdapter extends BaseAdapter {
    	private String[] mItems;
    	private LayoutInflater mInflater;
    	public ItemAdapter(Context context){
    		mItems = getResources().getStringArray(R.array.apps_main);
    		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	}
    	
		@Override
		public int getCount() {
			return mThumbs.length;
		}

		@Override
		public Object getItem(int position) {
			return mThumbs[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HolderView view;
			if(convertView == null){
				convertView = (LinearLayout) mInflater.inflate(R.layout.grid_items, null);
				view = new HolderView();
				view.title = (TextView) convertView.findViewById(R.id.title);
				view.icon = (ImageView) convertView.findViewById(R.id.icon);
				convertView.setTag(view);
			}else{
				view = (HolderView) convertView.getTag();
			}
			view.title.setText(mItems[position]);
			view.icon.setImageResource(mThumbs[position]);
			return convertView;
		}
		
		public class HolderView {
			
			private TextView title;
			private ImageView icon;
		}
    }
    
    private String getCurrentDateStr(){
		Date d = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		return sdf.format(d);
	}
    
    @Override
	public void onBackPressed() {
		showDialog(1);
	}
    
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog dialog = null;
		switch (id) {
		case 1:
			dialog = new AlertDialog.Builder(this).setTitle("提示").setMessage("是否确定退出?")
			.setPositiveButton("确定", new Dialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					setResult(Utils.EXIT_RESULT_CODE);
					PoliceMobileActivity.this.finish();
				}
			}).setNegativeButton("取消", null).create();
			break;

		default:
			break;
		}
		return dialog;
	}
	
	/*private void showDialog(){
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setMessage(R.string.login_msg);
		alert.setTitle(R.string.tip);
		alert.setPositiveButton(R.string.action_ok, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				finish();
			}
		});
		alert.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		alert.show();
	}*/
}